package com.ljx.reflect.reflect04;

import java.lang.reflect.Field;

/**
 * @author LJX
 * @Description: TODO
 * @Date: 2022/10/27 16:14
 * @Version 1.0
 */
public class Test01 {
	public static void main(String[] args)  throws Exception{
		// 获取class类对象
		Class<?> aClass = Class.forName("com.ljx.reflect.reflect04.Teacher");
		Field name = aClass.getDeclaredField("name");

		Field[] declaredFields = aClass.getDeclaredFields();
		for (Field declaredField : declaredFields) {
		}


		Field age = aClass.getField("age");

		// 只可以获取公共的属性
//		Field address = aClass.getField("address");

		Field[] fields = aClass.getFields();
		for (Field field : fields) {
		}


		// 为属性对象赋值
		// 通过反射类获取该类对象
		Object o = aClass.newInstance();
		Field name1 = aClass.getDeclaredField("name");
		name1.set(o,"李吉星");
		System.out.println(o);

		// 不能直接为私有属性赋值，		需要将address.setAccessible(true);设置为true

		Field address = aClass.getDeclaredField("address");
		address.setAccessible(true);
		address.set(o,"河南省，商丘市");
		System.out.println(o);
		// 通过get 方法获取值
		Object o1 = address.get(o);
		System.out.println(o1);
	}
}

class People{
	public int age;
	private int tel;
}

class Teacher extends People{
	public String name;
	private String address;

	@Override
	public String toString() {
		return "Teacher{" +
				"name='" + name + '\'' +
				", address='" + address + '\'' +
				'}';
	}
}